<?php

namespace falcon5\inc\Http\Controllers;

use falcon5\inc\Base\WPOption;
use falcon5\inc\Http\Views\ViewInterface;
use falcon5\inc\Infrastructure\Services\Falcon5Service;
use hollisho\helpers\ArrayHelper;
use hollisho\helpers\InvalidArgumentException;
use lucatume\DI52\ContainerException;

/**
 * @desc 页面控制器
 * Class PageController
 * @package falcon5\inc\Http\Controllers
 */
class PageController extends BaseController
{

    /**
     * @param ViewInterface $view
     * @return void
     * @throws ContainerException
     * @throws InvalidArgumentException
     * @desc
     */
    public function settings(ViewInterface $view)
    {
        /** @var WPOption $falcon5Option */
        $falcon5Option = falcon5_app(WPOption::class);
        $activityTicket = $falcon5Option->getApiKey();
        $originalLanguage = $falcon5Option->getOriginalLanguage();
        $selectedLanguages = $falcon5Option->getAllowedLanguages();
        $maxSupportedLanguages = 1;
        $engineSupportLang = [];
        try {
            /** @var Falcon5Service $service */
            $service = falcon5_app(Falcon5Service::class);
            $result = $service->supportLang();
            if (ArrayHelper::getValue($result, 'code') == 1) {
                $engineSupportLang = ArrayHelper::getValue($result, 'data');
            }
            //
            $result = $service->getPlatformSubscribe($activityTicket);
            if (ArrayHelper::getValue($result, 'code') == 1) {

                $data = ArrayHelper::getValue($result, 'data');
                $support_capacity = ArrayHelper::getValue($data, 'support_capacity', []);
                
                if ($support_capacity) {
                    //
                    $name = "";
                    foreach ($support_capacity as $value) {
                        if (isset($value["keyname"]) && ($value["keyname"] == "support_language_num")) {
                            $name = $value["name"];
                            break;
                        }
                    }
                    
                    $support_language_num=0;
                    if ($name) {
                        preg_match('/\((\d+)\)/', $name, $matches);
                        if (isset($matches[1])) {
                            $support_language_num = $matches[1]; // 结果: 100
                        }
                    }
                    if($support_language_num){
                        $maxSupportedLanguages=$support_language_num;
                    }
                }
            }
        } catch (\Exception $e) {
            falcon5_error_log('Caught exception: ' . $e->getMessage());
        }

        $protocol = falcon5_get_protocol();
        $settings['homeUrl'] = home_url('', $protocol);

        $getProtocolAndDomain = get_protocol_and_domain($settings['homeUrl']);
        // $protocol = ArrayHelper::getValue($getProtocolAndDomain, 'protocol');
        $domain = ArrayHelper::getValue($getProtocolAndDomain, 'domain');
        // 注意：get_protocol_and_domain 函数已经移除了 www 前缀，不需要再次处理

        $engineSupportLangMap = $engineSupportLang ? ArrayHelper::map($engineSupportLang, 'lang_code', 'display_name') : [];
        //var_dump($engineSupportLangMap);
        $view->assign([
            'engineSupportLang' => $engineSupportLang,
            'engineSupportLangMap' => $engineSupportLangMap,
            WPOption::FC5_API_KEY => $activityTicket,
            WPOption::ORIGINAL_LANGUAGE => $originalLanguage,
            WPOption::ALLOWED_LANGUAGES => $selectedLanguages,
            'falcon5SupportLang' => falcon5_support_languages(),
            'protocol' => $protocol,
            'domain' => $domain,
            "maxSupportedLanguages" => $maxSupportedLanguages
        ]);

        return $view->render();
    }
}
